package hit.example.clipbroad;

import java.util.ArrayList;

import com.telpoo.frame.ui.BaseFmActivity;

import hit.example.Db.DbAnhViet;
import hit.example.Object.ObjWord;
import hit.example.detail.DetailFm;
import hit.example.hitdict.R;
import hit.example.home.MyFragment;
import hit.example.home.TabId;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class ActivityClipbroad extends BaseFmActivity {
	View captureView;
	static String[] tabids = { TabId.home };
	public static ActivityClipbroad me;
	ListClipbroadFm listwordFm;
	DetailFm detailFm;
	MyFragment cruFragment;

	public static ActivityClipbroad getInstance() {
		return me;
	}

	public void pusListWordFm() {
		cruFragment = listwordFm;
		pushFragments(TabId.home, listwordFm, true, null);
		btnBack.setVisibility(View.GONE);
	}

	public void pushDetail(ObjWord ObjWord, int from) {
		cruFragment = detailFm;
		detailFm.setData(ObjWord, from);

		pushFragments(TabId.home, detailFm, true, null);
		btnBack.setVisibility(View.VISIBLE);

	}

	public ActivityClipbroad() {
		super(tabids, R.id.idview_clipbroad, "");

	}

	DbAnhViet db;
	ArrayList<ObjWord> list;
	Button btExit, btnBack;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		Intent intent = getIntent();
		String str = intent.getStringExtra("text");
		db = new DbAnhViet(getApplicationContext());
		list = new ArrayList<ObjWord>();
		list = db.get(str);
		if (list.size() > 0) {
			setContentView(R.layout.activity_view_clipbroad);
			captureView = findViewById(R.id.idLinearClipbroad);
			btExit = (Button) findViewById(R.id.idExit);
			btnBack = (Button) findViewById(R.id.idBack);
			me = ActivityClipbroad.this;
			detailFm = new DetailFm();

			

			listwordFm = new ListClipbroadFm(list);
			pusListWordFm();

			captureView = findViewById(R.id.idLinearClipbroad);
			btExit = (Button) findViewById(R.id.idExit);
			btnBack = (Button) findViewById(R.id.idBack);
			btExit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					finish();
				}
			});
			btnBack.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					listwordFm = new ListClipbroadFm(list);
					pusListWordFm();
				}
			});
		} else {

			finish();
		}

	}

	public void captureUsingDrawingCache() {

		captureView.buildDrawingCache();
		Bitmap b1 = captureView.getDrawingCache();
		// copy this bitmap otherwise distroying the cache will destroy
		// the bitmap for the referencing drawable and you'll not
		// get the captured view
		Bitmap b = b1.copy(Bitmap.Config.ARGB_8888, false);
		// BitmapDrawable d = new BitmapDrawable(b);
		// canvasView.setBackgroundDrawable(d);
		captureView.destroyDrawingCache();
		String saved = Images.Media.insertImage(this.getContentResolver(), b,
				"share", "picture");
		Intent shareIntent = new Intent();
		shareIntent.setAction(Intent.ACTION_SEND);
		shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(saved));
		shareIntent.setType("image/png");
		startActivity(Intent.createChooser(shareIntent, "-->"));
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

}
